﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :D_T_Advert_Exp.cs
//        description :广告信息处理类
//
//        created by 邹兴武 at  2011-01-05
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace huaguwang
{
    public class D_T_Advert_Exp : D_T_Advert
    {
        /// <summary>
        /// 查找分页数据
        /// </summary>
        /// <param name="begin">起始记录数</param>
        /// <param name="pagesize">页大小</param>
        /// <param name="title">搜索关键字:标题</param>
        /// <param name="catetitle">搜索关键字:类别名称</param>
        /// <param name="count">总记录数</param>
        /// <returns>查询结果数据</returns>
        public List<huaguwang.M_T_Advert_Exp.clsFind> GetList(int begin, int pagesize, string title, string mtitle,int  adsid ,  out int count)
        {
            var tab = @" dbo.T_Advert t1 join dbo.T_Billboard t2 on t1.AdsID = t2.id
                                join dbo.T_AdminUser t3 on t1.UserID = t3.id ";
            var con = "  t1.Flag = 1 and t2.Flag = 1 and t3.Flag = 1 ";
            if (!string.IsNullOrEmpty(title))
            {
                con += " and t1.Title like '%'+@title+'%' ";
            }
            if (!string.IsNullOrEmpty(mtitle))
            {
                con += " and t2.Module like '%'+@mtitle+'%' ";
            }
            if (adsid > 0)
            {
                con += " and t1.AdsID = @AdsID ";
            }

            SqlParameter[] parms = { 
                                   new SqlParameter("@title",SqlDbType.NVarChar,50),
                                   new SqlParameter("@mtitle",SqlDbType.NVarChar,50),
                                   new SqlParameter("@AdsID",SqlDbType.Int,4)
                                   };
            parms[0].Value = title.Exp_Trim();
            parms[1].Value = mtitle.Exp_Trim();
            parms[2].Value = adsid;

            var countSql = ExpandMSSQL.GetPageSQL(tab, con);
            count = MSSQLHelper.SQLConType.C1.Exp_GetSingle(
                countSql,
                parms
                ).Exp_IntTryParse();

            string strSQL = ExpandMSSQL.GetPageSQL(
                   begin,
                   pagesize,
                   "t1.id,t1.Title,t2.Title,t2.Module,t2.Plate,t2.Rules,t2.Price,t1.Topis,t1.EditDate,t3.LoginName,t1.States",
                   tab,
                   "t1.id",
                  con,
                   "t1.id desc"
                   );

            var List = MSSQLHelper.SQLConType.C1.Exp_GetList(
                strSQL,
                DR => new huaguwang.M_T_Advert_Exp.clsFind()
                {
                    id = DR["id"].Exp_IntTryParse(),
                    Title = DR["Title"].Exp_Trim(),
                    Module = DR["Module"].Exp_Trim(),
                    Plate = DR["Plate"].Exp_Trim(),
                    Rules = DR["Rules"].Exp_Trim(),
                    Price = ((int)DR["Price"].Exp_DecimalTryParse()).Exp_Trim(),
                    Topis = DR["Topis"].Exp_IntTryParse(),
                    UpDate = DR["EditDate"].Exp_DateTimeTryParse().ToString("yyyy-MM-dd"),
                    UserName = DR["LoginName"].Exp_Trim(),
                    State = DR["States"].Exp_BoolTryParse()?"使用中":"已停止"
                },
                parms
                );
            return List;
        }

        /// <summary>
        /// 批量更新广告图片状态
        /// </summary>
        public int UpdateState(List<int> list, bool flag)
        {
            var Refitem = MSSQLHelper.SQLConType.C1.ExecuteSqlTran((com, tran) =>
            {
                var intID = 0;
                var Gid = 0;
                string strSql = " update T_Advert set states=@states where id=@id ";
                SqlParameter[] parameters = {
                    new SqlParameter("@states", SqlDbType.Bit),
                    new SqlParameter("@id", SqlDbType.Int,4)};

                foreach (var id in list)
                {
                    parameters[0].Value = flag;
                    parameters[1].Value = id;
                    com.Parameters.Add(parameters[0]);
                    com.Parameters.Add(parameters[1]);
                    com.CommandText = strSql.ToString();
                    intID = com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    if (0 >= intID)
                    {
                        tran.Rollback();
                        return 0;
                    }
                    Gid++;
                }
                tran.Commit();
                return Gid;
            }
         );
            return Refitem;
        }
    }
}
